#!/bin/sh

# 检查程序是否在运行
is_exist(){
        # 获取PID
        PID=$(ps -ef |grep ${APP_NAME} | grep -v $0 |grep -v grep |awk '{print $2}')
        # -z "${pid}"判断pid是否存在，如果不存在返回1，存在返回0
        if [ -z "${PID}" ]; then
                # 如果进程不存在返回1
                return 1
        else
                # 进程存在返回0
                return 0
        fi
}

# 停止进程函数
stop(){
        is_exist
        if [ $? -eq "0" ]; then
                kill -9 ${PID}
                echo "${APP_NAME} process stop, PID=${PID}"
        else
                echo "There is not the process of ${APP_NAME}"
        fi
}

# 定义启动程序函数
start(){
        is_exist
        if [ $? -eq "0" ]; then
                echo "${APP_NAME} is already running, PID=${PID}"
        else
               nohup java   -jar  ${APP_NAME} --spring.profiles.active=dev  > app.log 2>&1 &
                PID=$(echo $!)
                echo "${APP_NAME} start success, PID=$!"
        fi
}

# 应用名称
APP_NAME=ceres-app-server.jar

echo "===========进入git目录============="
cd /home/ceres/code/ceres
echo "==================git fetch======================"
git checkout ceres20.06.01
git fetch

echo "==================git pull======================"
git pull


echo "===========打包jar包===================="
mvn clean install

echo "============删除旧的jar==================="
rm -rf /home/ceres/app/${APP_NAME}


echo "======拷贝======="
cp ceres-app-server/target/${APP_NAME} ../../app/


echo "======启动======="
cd ../../app
stop
start


